package process

import (
	"github.com/labstack/echo/v4"
	"github.com/labstack/echo/v4/middleware"
	"os"
	"pm/config"
	"strings"
)

const DefaultPort = "17456"


var processHttp  *echo.Echo

func RunProcessHttp(){
	processHttp = echo.New()
	//e.Pre(middleware.HTTPSRedirect())
	processHttp.Pre(middleware.CORS())
	processHttp.Use(middleware.Logger())
	//processHttp.Use(middleware.Recover())
	setRout()
	processHttp.Logger.Fatal(processHttp.Start(":" + DefaultPort))
}

func setRout(){
	processHttp.GET("/init", onInit)
	processHttp.POST("/run", run)
	processHttp.POST("/start", onStart)
	processHttp.POST("/delete", onDelete)
	processHttp.POST("/stop", onStop)
	processHttp.POST("/info", onInfo)
	processHttp.POST("/list", list)
	processHttp.POST("/sys/stop", sysStop)
	processHttp.POST("/sys/reload", sysReload)
}

func onInit(c echo.Context)error {
	return c.JSON(200, map[string]interface{}{
		"status": 0,
		"data":os.Getgid(),
	})
}

func sysStop(c echo.Context)error {
	os.Exit(0)
	return nil
}

func sysReload(c echo.Context)error {
	if err := config.ReloadConfig("/usr/local/pm/config.json");err != nil{
		return c.JSON(200, map[string]interface{}{
			"status": 1,
			"msg": err.Error(),
		})
	}
	return c.JSON(200, map[string]interface{}{
		"status": 0,
		"msg": "",
	})
}

func run(c echo.Context)error {
	name := c.FormValue("name")
	arg := c.FormValue("arg")
	args := strings.Split(arg, " ")
	if name == ""{
		return c.JSON(200, map[string]interface{}{
			"status": 1,
			"msg":"文件路径为空",
		})
	}

	if err := NewProcess2().Start(name, args, "");err != nil{
		return c.JSON(200, map[string]interface{}{
			"status": 1,
			"msg":err.Error(),
		})
	}
	return c.JSON(200, map[string]interface{}{
		"status": 0,
		"msg":"启动成功",
	})
}

func onStart(c echo.Context)error{
	key := c.FormValue("key")
	process := NewProcess2().GetProcess(key)
	if process == nil{
		return c.JSON(200, map[string]interface{}{
			"status": 0,
			"msg":"",
		})
	}
	process.Status = 0
	return c.JSON(200, map[string]interface{}{
		"status": 0,
		"msg":"",
	})
}

func onDelete(c echo.Context)error {
	key := c.FormValue("key")
	if key == ""{
		return c.JSON(200, map[string]interface{}{
			"status": 1,
			"msg":"参数错误",
		})
	}
	process := NewProcess2().GetProcess(key)
	if process == nil{
		return c.JSON(200, map[string]interface{}{
			"status": 0,
			"msg":"",
		})
	}
	if process.Status == 0{
		if err := NewProcess2().Stop(key);err != nil{
			return c.JSON(200, map[string]interface{}{
				"status": 1,
				"msg":err.Error(),
			})
		}

	}
	NewProcess2().DelProcess(key)
	return c.JSON(200, map[string]interface{}{
		"status": 0,
		"msg":"",
	})
}

func onStop(c echo.Context)error{
	key := c.FormValue("key")
	if key == ""{
		return c.JSON(200, map[string]interface{}{
			"status": 1,
			"msg":"参数错误",
		})
	}
	process := NewProcess2().GetProcess(key)
	if process == nil{
		return c.JSON(200, map[string]interface{}{
			"status": 1,
			"msg":"进程不存在",
		})
	}
	if err := NewProcess2().Stop(key);err != nil{
		return c.JSON(200, map[string]interface{}{
			"status": 1,
			"msg":err.Error(),
		})
	}
	return c.JSON(200, map[string]interface{}{
		"status": 0,
		"msg":"",
	})
}

func onInfo(c echo.Context)error{
	key := c.FormValue("key")
	if key == ""{
		return c.JSON(200, map[string]interface{}{
			"status": 1,
			"msg":"参数错误",
		})
	}
	process := NewProcess2().GetProcess(key)
	if process == nil{
		return c.JSON(200, map[string]interface{}{
			"status": 1,
			"msg":"进程不存在",
		})
	}
	return c.JSON(200, map[string]interface{}{
		"status": 0,
		"data":process,
	})
}

func list(c echo.Context)error{
	list := NewProcess2().GetAllProcess()
	return c.JSON(200, map[string]interface{}{
		"status": 0,
		"data":list,
	})
}
